import UserStore from '@/store/User';
import AlStore from '@/store/Alliance';
import AdminAuthTypes from '@/constants/AdminAuthTypes';
import StoreAuthTypes from '@/constants/StoreAuthTypes';
import _ from 'lodash';

const checkStore = rule_name => {
    if (rule_name == '*') return true;
    if (AlStore.info == null) return;
    const storeAuthRules = AlStore.info.store_auth_rules;
    if (AlStore.info.store_auth_rules === undefined) return false;
    const result = _.find(StoreAuthTypes, { name: rule_name });
    if (result === undefined) return false;
    const rule = `${result.id}`;
    return _.indexOf(storeAuthRules.split(','), rule) > -1;
};

const checkAdmin = rule_name => {
    if (rule_name == '*') return true;
    const adminAuthRules = UserStore.info.auth_rules;
    if (UserStore.info.auth_rules === undefined) return false;
    const result = _.find(AdminAuthTypes, { name: rule_name });
    if (result === undefined) return false;
    const rule = `${result.id}`;
    return _.indexOf(adminAuthRules.split(','), rule) > -1;
};

const check = (storeRule, adminRule) => {
    if (UserStore.info == null) return;
    //门店账号以及普通管理员账号需要验证
    if (UserStore.info.admin_type == 1 && UserStore.info.admin_auth_type == 1) {
        //联盟店超管不需要验证
        return true;
    }
    if (UserStore.info.admin_type == 1 && UserStore.info.admin_auth_type == 2) {
        //联盟店普通管理员权限验证
        return checkAdmin(adminRule);
    }
    if (UserStore.info.admin_type == 2 && UserStore.info.admin_auth_type == 1) {
        //门店权限验证
        return checkStore(storeRule);

    }
    if (UserStore.info.admin_type == 2 && UserStore.info.admin_auth_type == 2) {
        //门店普通管理员权限验证
        return checkStore(storeRule) && checkAdmin(adminRule);
    }
};

export default check;